import {
  Body,
  Controller,
  Delete,
  Get,
  Param,
  Patch,
  Post,
  Req,
} from '@nestjs/common';
import { ScriptListService } from './script-list.service';
import { CreateScriptListDto } from './dto/create-script-list.dto';
import { Request } from 'express';
import { UpdateScriptListDto } from './dto/update-script-list.dto';
import { Public } from 'src/common/decorator/public.decorator';
import { UpdateGroupToScriptListDto } from './dto/upadte-grouptoscript.dto';

@Controller('script-list')
export class ScriptListController {
  constructor(private readonly scriptListService: ScriptListService) {}

  @Post('createScript')
  createScript(
    @Body() createScriptListDto: CreateScriptListDto,
    @Req() request: Request,
  ) {
    const user = request.user;
    createScriptListDto.creator = (user as any).email;
    return this.scriptListService.createScript(createScriptListDto);
  }

  @Get('getAllScripts')
  getAllScripts() {
    return this.scriptListService.getAllScripts();
  }

  @Get('getScriptItemById/:id')
  getScriptItemById(@Param('id') id: string) {
    return this.scriptListService.getScriptItemById(id);
  }

  @Get('checkScriptById/:id')
  checkScriptById(@Param('id') id: string) {
    return this.scriptListService.checkScriptById(id);
  }

  @Get('getAllScriptItemByUser')
  getAllScriptItemByUser(@Req() request: Request) {
    const user = request.user;
    const email = (user as any).email;
    return this.scriptListService.getAllScriptItemByUser(email);
  }

  @Patch('updateScriptById/:id')
  updateScriptById(
    @Param('id') id: string,
    @Body() updateScriptListDto: UpdateScriptListDto,
  ) {
    return this.scriptListService.updateScriptById(id, updateScriptListDto);
  }

  @Delete('removeScriptById/:id')
  removeScriptById(@Param('id') id: string) {
    return this.scriptListService.removeScriptById(id);
  }

  @Get('isScriptItemExist/:scriptId')
  isScriptItemExist(@Param('scriptId') scriptId: string) {
    return this.scriptListService.isScriptItemExist(scriptId);
  }

  // 创建小组和脚本之间得关系
  @Post('createScriptItemToGroup/:groupuuid')
  createScriptItemToGroup(
    @Body() createScriptListDto: CreateScriptListDto,
    @Req() request: Request,
    @Param('groupuuid') groupuuid: string,
  ) {
    const user = request.user;
    createScriptListDto.creator = (user as any).email;
    return this.scriptListService.createScriptItemToGroup(
      createScriptListDto,
      groupuuid,
    );
  }

  @Get('getGroupToScriptItem/:scriptId')
  getGroupToScriptItem(@Param('scriptId') scriptId: string) {
    return this.scriptListService.getGroupToScriptItem(scriptId);
  }

  @Get('transformScriptToScence/:scriptId')
  transformScriptToScence(@Param('scriptId') scriptId: string) {
    return this.scriptListService.transformScriptToScence(scriptId);
  }

  // navigator.sendBeacon 无法携带 token jwt 验证
  @Public()
  @Post('updateScriptItemToGroup/:scriptId')
  updateScriptItemToGroup(
    @Param('scriptId') scriptId: string,
    @Body() updateGroupToScriptListDto: UpdateGroupToScriptListDto,
  ) {
    return this.scriptListService.updateScriptItemToGroup(
      scriptId,
      updateGroupToScriptListDto,
    );
  }

  @Get('unlockAllScripts')
  unlockAllScripts(@Req() request: Request) {
    const user = request.user;
    return this.scriptListService.unlockAllScripts((user as any).email);
  }

  @Get('getScriptItemByGroupuuid/:groupuuid')
  getScriptItemByGroupuuid(@Param('groupuuid') groupuuid: string) {
    return this.scriptListService.getScriptItemByGroupuuid(groupuuid);
  }

  @Delete('removeScriptItemFromGroup/:scriptTogroupuuid')
  removeScriptItemFromGroup(
    @Param('scriptTogroupuuid') scriptTogroupuuid: string,
  ) {
    return this.scriptListService.removeScriptItemFromGroup(scriptTogroupuuid);
  }

  @Get('getAllPublicScriptItem')
  getAllPublicScriptItem() {
    return this.scriptListService.getAllPublicScriptItem();
  }
}
